// pages/order/detail/index.js
var app = getApp(),
  core = app.requirejs('core'),
  order = app.requirejs('biz/order');
var diyad = app.requirejs('biz/ad');
var mainui = app.requirejs('mainui');
const api = require("../../../utils/api.js")
Page(app.sr.page({

  /**
   * 页面的初始数据
   */
  data: {
    cancel: order.cancelArray,
    cancelindex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      options: options
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this
    _this.initFunction()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  initFunction: function () {
    this.get_list();
    var $this = this;
    // 判断机型
    var isIpx = app.getCache('isIpx');
    if (isIpx) {
      $this.setData({
        isIpx: true,
        iphonexnavbar: 'fui-iphonex-navbar',
      })
    } else {
      $this.setData({
        isIpx: false,
        iphonexnavbar: '',
      })
    }
    /**启动广告 */
    diyad.get(this, function (res) {})
    // 小程序配置
    app.miniprogram(function (res) {
      //判断是否显示客服悬窗
      let globalData = app.globalData
      $this.setData({
        globalData: globalData
      })
    })
  },
  // 获取订单详情
  get_list: function () {
    var $this = this;
    $this.setData({
      loading: true
    });
    let apicontent = api.orders.orderDetail()
    core.get(apicontent, {
      ...$this.data.options
    }, function (res) {
      if (res.code != 0) {
        if (res.error != 50000) {
          core.toast(res.message, 'loading');
        }
      }
      if (res.code == 1) {
        res.data.order.createTime = $this.timeFun(res.data.order.createTime)
        $this.setData({
          ...res.data,
          // getOrder: JSON.stringify(res.data.order),
          goods: res.data.orderGoodsList,
          show: true,
        })
      }

    })
  },
  // 格式化时间
  timeFun(data) {
    var time = new Date(data)
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours() < 10 ? '0' + time.getHours() : time.getHours();
    var mm = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes();
    var s = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds();
    return y + '-' + m + '-' + d + ' ' + h + ':' + mm + ':' + s;
  },
  //格式化时间
  timeStamp(index) {
    if (index != '') {
      let time = new Date(index).getTime()
      return time
    }
    return ''
  },
  // 到秒
  timeStampFun() {
    let time = Date.parse(new Date())
    return time
  },
  // 广告关闭 
  closeAdver() {
    this.setData({
      showAdver: false
    })
  },
  // 取消订单
  cancel: function (e) {
    let _this = this
    let apicontent = api.orders.updateStatus()
    core.post(apicontent, {
      id: this.data.order.id,
    }, function (res) {
      wx.navigateBack({
        delta: 1,
      })
    })

    // wx.navigateTo({
    //   url: '/pages/order/index',
    // })
    // core(/order/api / order / delete)
    // 埋点
    // core.analytics('/order/detail', '订单详情', {
    //   ec: 'WeChat',
    //   ea: 'Click',
    //   el: '订单详情页::取消订单_' + this.data.order.ordersn
    // })
  },
  //支付订单
  pay: function (e) {
    var $this = this;
    let id = e.currentTarget.dataset.id;

    let detail = $this.data.order

    // core.analyticsPay('/order/detail', '订单详情', {
    //   ec: 'WeChat',
    //   ea: 'Purchase',
    //   el: detail.ordersn,
    //   pa: 'purchase',
    //   ti: detail.ordersn,
    //   tr: detail.price
    // }, goods)
    let memberId = app.getCache('userinfo_id');
    let apicontent = api.orders.prepay()
    core.post(apicontent, {
      orderId: detail.id,
      shopId: 1,
      memberId,
      rePay: 1,
    }, function (res) {
      if (res.code == 1) {
        setTimeout(() => {
          core.pay(res.data, function (res) {
            if (res.errMsg == "requestPayment:ok") {
              $this.initFunction()
            }
          });
        }, 1000)
      } else {
        core.alert(res.message)
      }
    })
    // 埋点
    // core.analytics('/order/detail', '订单详情', {
    //   ec: 'WeChat',
    //   ea: 'Click',
    //   el: '订单详情页::支付订单_' + $this.data.order.ordersn
    // })


  },
  // 查看物流信息
  checkexpress: function (e) {
    let expressid = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/order/express/index?id=' + expressid,
    })
  },
}))